import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
import { getManageTabSwitch, getSearchList } from '../mconfig';
const baseUrl = getBaseUrl();
// const baseUrl = 'http://192.168.153.246:31019'; //旭潮本地
// const baseUrl = '/deleGateTest';

const delegateManageService = (curRes: Record<string, any>) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}`, //流程类别
  ];

  /** 获取数据字典loadMdicMap */
  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = getManageTabSwitch(curRes);

  /** 获取搜索参数 */
  const searchListParams = getSearchList(loadMdicMapObj?.loadMapInfo, queryTypeObj);

  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };

      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, requestBody, {
        dicTransName: 'category',
        orgTransName: `${ISearchTranslateAll.DELEGATE_DEPT},${ISearchTranslateAll.AGENT_DEPT},${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.MODIFY_DEPT}`,
        // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      });

      const requestURL = `${baseUrl}/delegateController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, searchParams);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getDeleGateData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    if (request?.delegateTime) {
      const [delegateTimeStart, delegateTimeEnd] = request.delegateTime;
      request['startTime'] = delegateTimeStart.format('YYYY-MM-DD 00:00:00');
      request['endTime'] = delegateTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['delegateTime'];
    }

    delete request['assignFormNameDept'];
    delete request['assignFormNameUser'];
    if (request?.assignerID && request?.assignerID?.length > 0) {
      request['assignerID'] = request?.assignerID?.toString();
    } else {
      delete request['assignerID'];
    }

    delete request['agentFormNameDept'];
    delete request['agentFormNameUser'];
    if (request?.agenterID && request?.agenterID?.length > 0) {
      request['agenterID'] = request?.agenterID?.toString();
    } else {
      delete request['agenterID'];
    }

    run({
      ...request,
    });
  };

  /** 搜索列表  */
  const searchList = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [loadMdicMapObj?.loadMapInfo]);

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getDeleGateData,
    reloadRefresh: refresh,
    ...returnTableProps,
    ...searchList,
  };
};

export { delegateManageService };
